package org.example.elm_xiangmu.service.impl;


import org.example.elm_xiangmu.mapper.CartMapper;
import org.example.elm_xiangmu.mapper.OrderDetailetMapper;
import org.example.elm_xiangmu.mapper.OrdersMapper;
import org.example.elm_xiangmu.po.Business;
import org.example.elm_xiangmu.po.Cart;
import org.example.elm_xiangmu.po.OrderDetailet;
import org.example.elm_xiangmu.po.Orders;
import org.example.elm_xiangmu.result.Result;
import org.example.elm_xiangmu.service.BusinessService;
import org.example.elm_xiangmu.service.CartService;
import org.example.elm_xiangmu.service.OrderDetailService;
import org.example.elm_xiangmu.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.example.elm_xiangmu.service.impl.CartServiceImpl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl  implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderDetailServiceImpl orderDetailServiceImpl;
    @Autowired
    private BusinessService businessService;

    @Override
    public Integer createOrders(Orders orders) {
        String Date = orders.getOrderDate();
       Integer orderId = ordersMapper.createOrders(orders);
//        然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
        List<Cart> carts = cartService.listCart(orders.getUserId(), orders.getBusinessId());
        for (Cart cart : carts) {
            OrderDetailet orderDetailet = new OrderDetailet();
            orderDetailet.setOrderId(orders.getOrderId());
            orderDetailet.setQuantity(cart.getQuantity());
            orderDetailet.setFoodId(cart.getFoodId());
            orderDetailServiceImpl.saveOrderDetailet(orderDetailet);
        };
//        然后根据用户编号、商家编号删除购物车表中的数据。
        Cart cart = new Cart();
        cart.setUserId(orders.getUserId());
        cart.setBusinessId(orders.getBusinessId());
        cart.setFoodId(0);
        cartService.remove(cart);

        return ordersMapper.getOrderIdByDate(Date);


    }


    @Override
    public Result getOrdersById(Integer orderId) {
        Orders orders = ordersMapper.getOrdersById(orderId);
        orders.setBusiness(businessService.getBusinessById(orders.getBusinessId()));
        orders.setList(orderDetailServiceImpl.getOrderDetailetByOrderId(orderId));
        return Result.success(orders,"查询成功");
    }

    @Override
    public Result listOrdersByUserId(String userId) {
        List<Orders> ordersList = ordersMapper.listOrdersByUserId(userId);
        for (Orders order : ordersList) {
            // 设置商家信息
            order.setBusiness(businessService.getBusinessById(order.getBusinessId()));
            // 设置订单明细信息
            order.setList(orderDetailServiceImpl.getOrderDetailetByOrderId(order.getOrderId()));
        }
        return Result.success(ordersList,"查询成功");
    }
}
